import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
const moment = require('../../../utils/moment.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    pageNo: 1,
    list: [],
    hasNextPage: false,
    keyword: '',
    address: [],
    pageData: {},
    statusList: [
      {
        id: '',
        text: '全部'
      },
      {
        id: 0,
        text: '当天使用'
      },
      {
        id: 3,
        text: '三天内使用'
      },
      {
        id: 7,
        text: '七天内使用'
      },
      {
        id: -7,
        text: '超过7天没有使用'
      }
    ],
    status: ''
  },
  onLoad: function () {
    this.getlist()
    this.getListType()
  },
  onShow: function () {
    // this.getlist()
  },

  goDeviceDetail(e) {
    const typeId = e.currentTarget.dataset.typeId;
    const id = e.currentTarget.dataset.id;
    wx.setStorageSync('devId', id)
    let deviceTypeName = this.data.listType.filter(item => item.id == typeId)[0].typeName
    console.log('deviceTypeName', deviceTypeName)
    let isMini = deviceTypeName.indexOf('Mini') != -1
    wx.setStorageSync('isMini', isMini)
    wx.navigateTo({
      url: `/pages/sub_menu/index?typeId=${typeId}&devId=${id}`,
    })
  },

  copyContent(e) {
    const cpoyContent = e.currentTarget.dataset.content;
    wx.setClipboardData({
      //去找上面的数据
      data: cpoyContent,
      success: function (res) {
        wx.showToast({
          title: '已复制详细地址',
        });
      }
    });
  },

  handleEdit(e) {
    const devId = e.currentTarget.dataset.deviceId
    wx.navigateTo({
      url: '/pages/device_edit/index?devId=' + devId,
    })
  },

  //查询列表
  getlist: function () {

    let params = {
      keyword: this.data.keyword,
      pageNo: this.data.pageNo,
      distance: 1
    }
    if (this.data.status !== '') {
      params.dayFilter = this.data.status
    }
    console.log(this.data.status);
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/findList',
      method: 'POST',
      data: params
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      list.map(item => {
        //  到期时间
        let redTime = moment(item.createDate).add(7, 'd');
        if (redTime.isBefore(moment())) {
          item.isRed = true;
        }
        return item;
      })

      this.setData({
        list: list,
        pageData: res.page,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });

    })
  },

  getListType: function() {
    var params = {
      keyword: '', 
      pageNo: 1,
      pageSize: 100
    }
     
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/findTypeList',
      method: 'POST',
      data:params
    }, (res) => {
      let list = res.list;
      this.setData({
        listType: list
      });
    })
  },


  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },

  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },

  seachInputVal: function (e) {
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },






  //删除
  handlDelet: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: '确定要解绑该设备？',
      success: function (res) {
        if (res.confirm) {
          that.delectRequest(id)
        }
      }
    })
  },


  delectRequest: function (id) {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + "/gds/protected/device/unbind",
      method: 'POST',
      data: {
        id
      }
    }, (res) => {
      console.log(res)
      wx.showToast({
        title: "解绑成功",
      })
      that.getlist()
    })
  },


  //********************筛选更多********************
  popupRight: function () {
    this.setData({
      showPopup: true
    })
  },

  popupCancel: function () {
    this.setData({
      showPopup: false
    })
  },
  //重置
  popupReset: function () {
    this.setData({
      status: '',
    })
  },

  popupConfirm: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
    this.popupCancel();
  },
  // 选择状态
  checkStatus: function (e) {
    var status = e.currentTarget.dataset.id
    this.setData({
      status
    })
  },

  //********************筛选更多end********************//



})